home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: munta.cs.mu.OZ.AU!baillie
- From: baillie@munta.cs.mu.OZ.AU (Stephen Baillie)
- Subject: Re: How to test a random generator ?
- Message-ID: <9603010.14743@mulga.cs.mu.OZ.AU>
- Sender: news@cs.mu.OZ.AU (CS-Usenet)
- Organization: Computer Science, University of Melbourne, Australia
- References: <4e2q7g$oe@centre.univ-orleans.fr> <4e933p$t25@longwood.cs.ucf.edu>
- Date: Mon, 29 Jan 1996 23:10:50 GMT
-
- schnitzi@longwood.cs.ucf.edu (Mark Schnitzius) writes:
-
- >emmguyot@desiree.univ-orleans.fr (Emmanuel GUYOT) writes:
-
- >>How can I test a random generator ?
- >>I need to verify that my random generator is really
- >>random. Can you tell me a way to test this ?
-
- >Every random number algorithm I've ever seen repeats
- >after a given number of digits. Good ones take a
- >long time to repeat. See how many digits it takes
- >before yours starts to repeat.
-
- >If I remember correctly, there's no way to tell if
- >a given sequence is truly random. I think there was
- >something about it in The Turing Omnibus by A. K.
- >Dewdney.
-
- There are various statistical tests you can use to determine if a
- random number generator exhibits certain properties of truly
- random sequences. The most common/appropriate/applicable is
- probably the chi squared test. Basically, you take n random
- numbers from the range a - b, then take the sum of the squares
- of the differences between the actual occurances of each number
- and the expected or average number, take the square root, and
- the result should be roughly b-a. Too close to zero and it's
- not random enough; too large and it shows too much bias towards
- particular numbers. An example:
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
-
- /*
- * chi2.c - a program to apply a chi squared test to a random number
- * generator, to see how "random" it is.
- * Gives the most meaningful values for lots of iterations.
- * Some years ago I stole the basic theory from a textbook, but I've
- * forgotten which one. This is all off the top of my head.
- * Written by Stephen Baillie, 30/1/96.
- */
-
- int main(int argc, char *argv[]);
- int main(int argc, char *argv[])
- {
- long *counts;
- long range, iterations, i;
- double chi2, average;
-
- if (argc != 3)
- { /* Check parameters and report correct usage */
- printf("Usage: chi2 range iterations\n");
- exit(1);
- }
-
- range = atol(argv[1]);
- iterations = atol(argv[2]);
- if (range < 2)
- { /* Having only one possible result is not random. < 1 is silly. */
- printf("Error: Range must be at least 2.\n");
- exit(1);
- }
- if (iterations < range)
- { /* Fairly arbitrary cut off value - got a better one? */
- printf("Value of iterations too small for meaningful result.\n");
- exit(1);
- }
- counts = (long *) malloc(range * sizeof(*counts));
- if (counts == NULL)
- { /* Can't continue without space for our results array... */
- printf("Memory allocation failure; wanted %ld bytes.\n",
- range * sizeof(*counts));
- exit(1);
- }
-
- for (i = 0; i < range; i++)
- counts[i] = 0; /* Start off with no hits. */
-
- for (i = 0; i < iterations; i++)
- counts[random() % range]++; /* Insert your random function here */
-
- /* Calculate the chi squared value, the sum of the squares of the
- deviations from the expected value */
- average = (double) iterations / (double) range;
- for (chi2 = 0, i = 0; i < range; i++)
- chi2 += (counts[i] - average) * (counts[i] - average);
-
- /* Report the chi value, and a very rough assessment of the RNG */
- printf("Chi value = %f ", sqrt(chi2));
- if (sqrt(chi2) < range / 2)
- printf("(not random enough)\n");
- else if (sqrt(chi2) > range * 2)
- printf("(too biased)\n");
- else
- printf("(not bad)\n");
-
- free(counts);
-
- exit(0);
- } /* main */
-
- Hope this is vaguely useful,
-
- Steve.
-
- --
- "If we shadows have offended, Think but this and all is mended:
- That you have but slumbered here, While these visions did appear.
- And this weak and idle theme, no more yielding but a dream."
- baillie@munta.cs.mu.oz.au (Stephen Baillie)
-